﻿#include "AvsSpectroMeterUtils.h"
#include <fstream>
#include "BaseLineRemove.h"
#include "WaitUtils.h"

AvsSpectroMeterUtils::AvsSpectroMeterUtils(QObject *parent) : QObject(parent)
{

}

AvsMeasResult AvsSpectroMeterUtils::Parse(QString filename)
{
    ifstream inFile(filename.toLocal8Bit().data(), ios::in | ios::binary);
    AvspecInfo *avsinfo = NULL;
    char *buf = new char[sizeof(AvspecInfo)];

    inFile.read( buf, sizeof(AvspecInfo));
    avsinfo = (AvspecInfo*)buf;
    qDebug()<<__FUNCTION__<<__LINE__<<avsinfo->measconf.m_StopPixel<<" "<<avsinfo->measconf.m_StartPixel;
    int size = avsinfo->measconf.m_StopPixel - avsinfo->measconf.m_StartPixel + 1;

    float *xcoord = new float[size];
    float *scope = (float*)malloc(size*sizeof(float));
    float *dark = (float*)malloc(size*sizeof(float));
    float *reference = (float*)malloc(size*sizeof(float));

    inFile.read( (char*)(xcoord), size * sizeof(float));
    inFile.read( (char*)(scope), size * sizeof(float));
    inFile.read( (char*)(dark), size * sizeof(float));
    inFile.read( (char*)(reference), size * sizeof(float));

    AvsMeasResult ret;
    ret.SerialId = filename;
    for(int i = 0; i < size; i++) {
        ret.resultData.append(QPointF(xcoord[i],scope[i]));
    }

    delete [] xcoord;
    delete [] scope;
    delete [] dark;
    delete [] reference;
    delete [] buf;

    return ret;
}

double AvsSpectroMeterUtils::AutoCalcIntegrationTime(BaseSpectroMeter *meter)
{
    for(int i=2;i<100;i++)
    {
        meter->StartMeasure();

//        meter->read
    }
    return 5.00;
}
